<?php
require_once dirname(__FILE__).'/../Plugin.php';

class Plugin_LinkMenuPrimary_Class extends My_Plugin
{
    function  __construct($wertex)
    {
        parent::__construct('link_menu_primary', $wertex);
        $this->_initForm();
    }

    function action()
    {
        $post = $this->request->getPost();
        if ($this->request->isPost() AND $post[$this->getPrefix().'submit']) {
            if($this->add()){
                return true;
            }
        }
    }

    function getContent(){
        $row = $this->getLastRow();

        return $this->toTemplate(
            array(
                'LABEL' => $row['label'],
                'HREF' => parent::getUrl().'public/'.$row['controller'].'/'.$row['action'],
                'TITLE' => $row['title']
            ), 'index'
        );
    }

    function getHeader(){
        $row = $this->getLastRow();
        return $row['title'];
    }

    function add()
    {
        $values = $this->request->getPost();
        if ($this->form->isValid($values)) {
            $insertData = array();
            foreach ($this->getCols() AS $column) {
                $insertData[$column] = $values[$this->getPrefix().$column];
            }
            if($this->insert($insertData)){
                $this->setMessage('Veiksmīgi saglabāts');
                return $this->getLastId();
            }
        } else {
            $this->form->populate($values);
        }
    }

    function body()
    {
        $content = file_get_contents(dirname(__FILE__).'/template/form.html');
        $content = str_replace('{FORM}', $this->form, $content);
        return $content;
    }

    function _initForm()
    {
        $this->form = new Zend_Form;
        $this->form->addElement('hidden', $this->getPrefix().'wertex');
        $this->form->addElement('hidden', $this->getPrefix().'date');

        $this->form->addElement('text', $this->getPrefix().'label', array(
            'label' => 'Label',
            'required' => true,
            'filters' => array(
                'StringTrim'
            ),
            'validators' => array(
                array('StringLength', false, array(0, 150))
            ),
        ));

        $this->form->addElement('text', $this->getPrefix().'controller', array(
            'label' => 'Controller',
            'required' => false,
            'filters' => array(
                'StringTrim'
            ),
            'validators' => array(
                array('StringLength', false, array(0, 150))
            ),
        ));

        $this->form->addElement('text', $this->getPrefix().'action', array(
            'label' => 'Action',
            'required' => false,
            'filters' => array(
                'StringTrim'
            ),
            'validators' => array(
                array('StringLength', false, array(0, 150))
            ),
        ));

        $this->form->addElement('text', $this->getPrefix().'title', array(
            'label' => 'Title',
            'required' => false,
            'filters' => array(
                'StringTrim'
            ),
            'validators' => array(
                array('StringLength', false, array(0, 150))
            ),
        ));


        $this->form->addElement('submit', $this->getPrefix().'submit',
            array(
                'ignore'   => true,
                'label'    => 'Saglabāt',
            )
        );

        $lastRow = $this->getLastRow();
        $this->form->setDefault($this->getPrefix().'label', $lastRow['label']);
        $this->form->setDefault($this->getPrefix().'controller', $lastRow['controller']);
        $this->form->setDefault($this->getPrefix().'action', $lastRow['action']);
        $this->form->setDefault($this->getPrefix().'title', $lastRow['title']);

        $this->form->setDefault($this->getPrefix().'wertex', $this->getWertex());
        $this->form->setDefault($this->getPrefix().'date', date("Y-m-d H:i:s"));
    }

}
